<html>
<head><meta charset="utf-8"><title>deduplicate query cache · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html">deduplicate query cache</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226237011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226237011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226237011">(Feb 13 2021 at 09:50)</a>:</h4>
<p>We currently call const eval multiple times while expecting exactly the same result: <a href="https://github.com/rust-lang/rust/blob/21cbbdc44de84e3ea99bca239091e5d1c49af654/compiler/rustc_mir/src/const_eval/eval_queries.rs#L245-L262">https://github.com/rust-lang/rust/blob/21cbbdc44de84e3ea99bca239091e5d1c49af654/compiler/rustc_mir/src/const_eval/eval_queries.rs#L245-L262</a></p>



<a name="226237084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226237084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226237084">(Feb 13 2021 at 09:53)</a>:</h4>
<p>with <a href="https://github.com/rust-lang/rust/issues/81339">#81339</a> this would get even worse, as the call with <code>Reveal::All</code> now just tries to reuse <code>Reveal::UserFacing</code> which  tries to reuse <code>Reveal::Selection</code>. see <a href="https://github.com/rust-lang/rust/pull/81339#issuecomment-778592146">https://github.com/rust-lang/rust/pull/81339#issuecomment-778592146</a></p>



<a name="226237090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226237090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226237090">(Feb 13 2021 at 09:53)</a>:</h4>
<p>the same issue also exists for <code>WithOptConstParam</code>, for which we pretty much do the exact same thing</p>



<a name="226237137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226237137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226237137">(Feb 13 2021 at 09:54)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/21cbbdc44de84e3ea99bca239091e5d1c49af654/compiler/rustc_mir_build/src/build/mod.rs#L29-L31">https://github.com/rust-lang/rust/blob/21cbbdc44de84e3ea99bca239091e5d1c49af654/compiler/rustc_mir_build/src/build/mod.rs#L29-L31</a></p>



<a name="226237169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226237169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226237169">(Feb 13 2021 at 09:55)</a>:</h4>
<p>i think it's not a big issue for the in-memory query cache cause we pretty much always just store interned stuff</p>



<a name="226237226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226237226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226237226">(Feb 13 2021 at 09:57)</a>:</h4>
<p>but for example <code>eval_to_const_value_raw</code> is also cached on disk which goes through <code>Encodable</code> i think. So we have deep clones there afaict</p>



<a name="226237284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226237284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226237284">(Feb 13 2021 at 09:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/deduplicate.20query.20cache/near/226237226">said</a>:</p>
<blockquote>
<p>but for example <code>eval_to_const_value_raw</code> is also cached on disk which goes through <code>Encodable</code> i think. So we have deep clones there afaict</p>
</blockquote>
<p>cc <span class="user-group-mention" data-user-group-id="3282">@wg-incr-comp</span> does one of you have an idea on how we can improve this? Or well, I guess the question is.. is my understanding even correct here ^^</p>



<a name="226243102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226243102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226243102">(Feb 13 2021 at 12:36)</a>:</h4>
<p>Some queries (typeck) allocate manually their result on an arena, so as to return the same pointer when the result is the same. This helps when the original result takes a lot of memory. However, this does not deduplicate on-disk. For this, an interning scheme is required.</p>



<a name="226243258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226243258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226243258">(Feb 13 2021 at 12:40)</a>:</h4>
<p>A few things:</p>
<ol>
<li>for <code>const</code> and <code>static</code> items, we should not be storing <code>Reveal::UserFacing</code> and <code>Reveal::Selection</code> at all, as all cross-crate const evaluations of non-generic <code>const</code> items should go through <code>Reveal::All</code> afaict</li>
<li>even for generic (so, associated or anonymous) constants there may be cases where encoding anything but <code>Reveal::All</code> is necessary at all</li>
<li>we already deduplicate <code>AllocId</code>s (and thus their <code>Allocation</code>), not sure if we also do this with plain <code>&amp;'tcx Allocation</code>s, so we should look into that.</li>
<li>Is <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.ConstKind.html">ConstKind</a> what we need to consider when deciding whether to create an interning scheme for on-disk  serialization? Or should we do that at the <code>&amp;'tcx ty::Const&lt;'tcx&gt;</code> level? The latter has the advantage of  being very cheap to check for duplicates.</li>
</ol>



<a name="226244154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226244154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226244154">(Feb 13 2021 at 13:04)</a>:</h4>
<blockquote>
<ol>
<li>for <code>const</code> and <code>static</code> items, we should not be storing <code>Reveal::UserFacing</code> and <code>Reveal::Selection</code> at all, as all cross-crate const evaluations of non-generic <code>const</code> items should go through <code>Reveal::All</code> afaict</li>
</ol>
</blockquote>
<p>hmm, i guess using erroneous concrete constants should always emit an error? Or at least a <code>const_err</code> lint? So you're not supposed to get these consts into associated constants used in generic anon consts?</p>
<blockquote>
<ol start="2">
<li>even for generic (so, associated or anonymous) constants there may be cases where encoding anything but <code>Reveal::All</code> is necessary at all</li>
</ol>
</blockquote>
<p>do you have something <em>concrete</em> in mind here (words <span aria-label="sparkles" class="emoji emoji-2728" role="img" title="sparkles">:sparkles:</span>)</p>
<blockquote>
<ol start="3">
<li>we already deduplicate <code>AllocId</code>s (and thus their <code>Allocation</code>), not sure if we also do this with plain <code>&amp;'tcx Allocation</code>s, so we should look into that.</li>
</ol>
</blockquote>
<p><span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span> </p>
<blockquote>
<ol start="4">
<li>Is <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.ConstKind.html">ConstKind</a> what we need to consider when deciding whether to create an interning scheme for on-disk  serialization? Or should we do that at the <code>&amp;'tcx ty::Const&lt;'tcx&gt;</code> level? The latter has the advantage of  being very cheap to check for duplicates.</li>
</ol>
</blockquote>
<p>we could just implement <code>DecodeWithShorthand</code> for <code>ConstKind</code> <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="226244158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226244158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226244158">(Feb 13 2021 at 13:04)</a>:</h4>
<p>that's actually somethingI can just try rn</p>



<a name="226244395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226244395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226244395">(Feb 13 2021 at 13:11)</a>:</h4>
<p>for 2. I left a comment on the PR</p>



<a name="226244403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226244403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226244403">(Feb 13 2021 at 13:11)</a>:</h4>
<p>repeated here: we don't need to encode the value of generic constants, since we can just recompute them</p>



<a name="226244407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226244407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226244407">(Feb 13 2021 at 13:12)</a>:</h4>
<p>as their MIR must be encoded anyway</p>



<a name="226244674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226244674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226244674">(Feb 13 2021 at 13:19)</a>:</h4>
<p>ah, yeah. we don't <em>have</em> to encode anything that can just be recomputed</p>



<a name="226244719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226244719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226244719">(Feb 13 2021 at 13:20)</a>:</h4>
<p>we may still <em>want</em> to though <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> that's where the heuristic comes into play <span aria-label="sparkles" class="emoji emoji-2728" role="img" title="sparkles">:sparkles:</span></p>



<a name="226265187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226265187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226265187">(Feb 13 2021 at 20:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/deduplicate.20query.20cache/near/226244158">said</a>:</p>
<blockquote>
<p>that's actually somethingI can just try rn</p>
</blockquote>
<p>tried it in <a href="https://github.com/rust-lang/rust/issues/82059">#82059</a> and it seemed to slightly decrease perf</p>



<a name="226265224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226265224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226265224">(Feb 13 2021 at 20:29)</a>:</h4>
<p>without a noticeable impact on max-rss</p>



<a name="226297395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297395">(Feb 14 2021 at 10:27)</a>:</h4>
<p>I would not really expect max-rss to change, but the size of metadata to decrease</p>



<a name="226297463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297463">(Feb 14 2021 at 10:29)</a>:</h4>
<p>but it actually looks like max-rss got worse in incremental builds?</p>



<a name="226297479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297479">(Feb 14 2021 at 10:30)</a>:</h4>
<p>if we have a slot of small improvements</p>



<a name="226297521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297521">(Feb 14 2021 at 10:30)</a>:</h4>
<p>like when caching <code>ConstKind::Value</code></p>



<a name="226297535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297535">(Feb 14 2021 at 10:30)</a>:</h4>
<p>the gains from the improved encoding are probably overshadowed by the additional cache</p>



<a name="226297544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297544">(Feb 14 2021 at 10:31)</a>:</h4>
<p>I wonder if doing that deduplication at the <code>ty::Const</code> level would make more sense</p>



<a name="226297546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297546">(Feb 14 2021 at 10:31)</a>:</h4>
<p>as that is already interned</p>



<a name="226297554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297554">(Feb 14 2021 at 10:31)</a>:</h4>
<div class="codehilite" data-code-language="Bash"><pre><span></span><code>du --summarize --bytes 3158857297417566824631a85c4cb3c0615ec6c2 c10327bae5477ad75e13ebbc684a251969773716
<span class="m">404573596</span>   3158857297417566824631a85c4cb3c0615ec6c2
<span class="m">403608352</span>   c10327bae5477ad75e13ebbc684a251969773716
</code></pre></div>



<a name="226297605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297605">(Feb 14 2021 at 10:32)</a>:</h4>
<p>so it did have an effect after all, but it's very expensive to get that small gain</p>



<a name="226297613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/deduplicate%20query%20cache/near/226297613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/deduplicate.20query.20cache.html#226297613">(Feb 14 2021 at 10:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/deduplicate.20query.20cache/near/226297544">said</a>:</p>
<blockquote>
<p>I wonder if doing that deduplication at the <code>ty::Const</code> level would make more sense</p>
</blockquote>
<p>that might work</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>